package com.langyiquan.backed.controller;

import com.langyiquan.bo.CarouselBO;
import com.langyiquan.exception.http.ParamException;
import com.langyiquan.pojo.Carousel;
import com.langyiquan.service.CarouselService;
import com.langyiquan.utils.UnifyResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


@Api(value = "轮播图", tags = {"轮播图的相关接口"})
@RestController
@RequestMapping("carousel")
public class CarouselController extends BaseController {

    @Autowired
    public CarouselService carouselService;

    @ApiOperation(value = "获取轮播图信息",notes = "获取轮播图信息",httpMethod = "GET")
    @GetMapping("list")
    public UnifyResponse list(@RequestParam Date startTime,@RequestParam Date endTime){
        List<Carousel> list = carouselService.queryAll(startTime,endTime);
        return UnifyResponse.success(list);
    }

    @ApiOperation(value = "获取轮播图详情",notes = "获取轮播图详情",httpMethod = "GET")
    @GetMapping("info")
    public UnifyResponse info(@RequestParam Integer carouselId){
        if(carouselId == null){
            throw new ParamException(8001);
        }
        Carousel carousel = carouselService.queryCarouselInfo(carouselId);
        return UnifyResponse.success(carousel);
    }

    @ApiOperation(value = "新增轮播图", notes = "新增轮播图", httpMethod = "POST")
    @PostMapping("/insert")
    public UnifyResponse saveDept(@RequestBody CarouselBO carouselBO) {
        System.out.println(carouselBO.getTargetId());
        carouselService.createCarousel(carouselBO);
        return UnifyResponse.success();
    }

    @ApiOperation(value = "更新轮播图", notes = "更新轮播图", httpMethod = "POST")
    @PostMapping("/update")
    public UnifyResponse updateDept(@RequestBody CarouselBO carouselBO) {
        carouselService.updateCarousel(carouselBO.getCarouselId(), carouselBO);
        return UnifyResponse.success();
    }


    @ApiOperation(value = "删除轮播图", notes = "删除轮播图", httpMethod = "POST")
    @PostMapping("/delete")
    public UnifyResponse deleteDept(@RequestParam Integer carouselId) {
        if(carouselId == null){
            throw new ParamException(8001);
        }
        carouselService.deleteCarousel(carouselId);
        return UnifyResponse.success();
    }

}
